﻿// ModuleBrowserDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ModuleBrowserDialog.h"
#include "afxdialogex.h"


// CModuleBrowserDialog 对话框

IMPLEMENT_DYNAMIC(CModuleBrowserDialog, CDialogEx)

CModuleBrowserDialog::CModuleBrowserDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MODULE_BROWSER, pParent)
{

}

CModuleBrowserDialog::~CModuleBrowserDialog()
{
}

void CModuleBrowserDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_MODULE, m_list);
}


BEGIN_MESSAGE_MAP(CModuleBrowserDialog, CDialogEx)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_MODULE, &CModuleBrowserDialog::OnLvnItemchangedListModule)
	ON_BN_CLICKED(IDOK, &CModuleBrowserDialog::OnBnClickedOk)
END_MESSAGE_MAP()


// CModuleBrowserDialog 消息处理程序


BOOL CModuleBrowserDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_MODULE_BROWSER_32))
	{
		m_il.Create(32, 32, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		m_list.SetImageList(&m_il, TVSIL_NORMAL);
	}

	Populate();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CModuleBrowserDialog::OnLvnItemchangedListModule(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	if (pNMLV->iItem>=0)
	{
		CString strDllName = m_list.GetItemText(pNMLV->iItem, 0);
		int len = strDllName.GetLength();
		if (len > 4)
		{
#ifdef _DEBUG
			m_strModName = strDllName.Left(len - 5);
#else
			m_strModName = strDllName.Left(len - 4);
#endif
		}
	}
	else
	{
		m_strModName.Empty();
	}

	*pResult = 0;
}


void CModuleBrowserDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}

void CModuleBrowserDialog::Populate()
{
	ck::RunTime* pRunTime = ck::RunTime::GetInstance();
	const char* szModHome = pRunTime->GetModuleHome();

	char szFilter[_MAX_PATH];
	memset(szFilter, 0, _MAX_PATH);
	_makepath(szFilter, NULL, szModHome, "mod_*", "dll");

	WIN32_FIND_DATA fd;
	HANDLE hFind = FindFirstFile(szFilter, &fd);

	if (INVALID_HANDLE_VALUE != hFind)
	{
		do
		{
			if (fd.cFileName[0] == '.')
			{
				continue;
			}
			else if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{

			}
			else
			{
				m_list.InsertItem(m_list.GetItemCount(), fd.cFileName, 1);
			}


		} while (FindNextFile(hFind, &fd));
	}

	FindClose(hFind);

}